﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MDMAPI.Service;
using MDMAPI.Models;
using MDMAPI.Filter;

namespace MDMAPI.Controllers
{
    [RequestAuthorize]
    [LogFilter]
    public class UserInfoController : ApiController
    {
        private UserInfoService userInfoService = new UserInfoService();

        /// <summary>
        /// 查询所有用户
        /// </summary>
        /// <returns></returns>
        public QueryResult GetListAll()
        {
            var userlist = userInfoService.GetListAll();
            QueryResult userResult = new QueryResult();
            userResult.Count = userlist.Count();
            userResult.UserInfoList = userlist.ToList();
            return  userResult;
        }

        public QueryResult GetUserInfoByMobile(string mobile)
        {
            var userlist = userInfoService.GetUserInfoByMobile(mobile);
            QueryResult userResult = new QueryResult();
            userResult.Count = userlist.Count();
            userResult.UserInfoList = userlist.ToList();
            return ( userResult);
        }

        /// <summary>
        /// 根据电话更新用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ExecuteResult Update([FromBody] UserInfo userInfo)
        {
            ExecuteResult userResult = new ExecuteResult();
            int i = userInfoService.Update(userInfo);
            if (i > 0)
            {
                userResult.Result = i;
                userResult.IsSuccess = true;
                userResult.Message = "更新成功";
            }
            else
            {
                userResult.Result = i;
                userResult.IsSuccess = false;
                userResult.Message = "更新失败";
            }
            return userResult;
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ExecuteResult Add([FromBody]UserInfo userInfo)
        {
            ExecuteResult userResult = new ExecuteResult();
            int i = userInfoService.Add(userInfo);

            if (i > 0)
            {
                userResult.Result = i;
                userResult.IsSuccess = true;
                userResult.Message = "添加成功";
            }
            else if (i == -1)
            {
                userResult.Result = i;
                userResult.IsSuccess = false;
                userResult.Message = "该电话已经被注册过";
            }
            else  
            {
                userResult.Result = i;
                userResult.IsSuccess = false;
                userResult.Message = "添加失败";
            }
            return userResult;
        }

        /// <summary>
        /// 根据电话，删除用户数据
        /// </summary>
        /// <param name="userInfo"></param>
        /// <returns></returns>
        [HttpPost]
        public ExecuteResult Remove([FromBody]UserInfo userInfo)
        {
            ExecuteResult userResult = new ExecuteResult();
            int i = userInfoService.Remove(userInfo.Mobile);

            if (i > 0)
            {
                userResult.Result = i;
                userResult.IsSuccess = true;
                userResult.Message = "删除成功";
            }
            else
            {
                userResult.Result = i;
                userResult.IsSuccess = false;
                userResult.Message = "删除失败";
            }
            return userResult;
        }
    }
}
